package com.liang.demo.controller;

import com.liang.demo.base.BaseApiController;
import com.liang.demo.base.RestResponse;
import com.liang.demo.base.SystemCode;
import com.liang.demo.po.Admin;
import com.liang.demo.po.Role;
import com.liang.demo.service.AdminService;
import com.liang.demo.service.MenuService;
import com.liang.demo.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 陈琪亮
 * @version 1.0
 * @date 2021/3/18 16:39
 */
@RestController
public class AdminController extends BaseApiController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;


    @RequiresAuthentication
    @GetMapping("/api/admin/info")
    public RestResponse getInfo() {
        Long adminId = getCurrentUser();
        if (adminId == null) {
            return RestResponse.fail(SystemCode.UNAUTHORIZED.getCode(), SystemCode.UNAUTHORIZED.getMessage());
        }
        Admin admin = adminService.getAdminInfo(adminId);
        Map<String, Object> data = new HashMap<>();
        //获取用户名
        data.put("username", admin.getNickName());
        //获取菜单
        data.put("menus", menuService.getMenuList(adminId));
        //获取角色
        List<Role> roleList = roleService.getRoleList(adminId);
        if (roleList != null && roleList.size() > 0) {
            List<String> roles = roleList.stream().map(Role::getName).collect(Collectors.toList());
            data.put("roles", roles);
        }
        //获取用户头像
        data.put("icon", admin.getIcon());
        return RestResponse.ok(data);
    }

}
